home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Mac Game Programming Gurus / TricksOfTheMacGameProgrammingGurus.iso / More Source / C⁄C++ / Kant Generator Pro 1.2 / src / Shell ƒ / apple events.c < prev    next >
C/C++ Source or Header  |  1994-11-15  |  3KB  |  126 lines

  1. #include "AppleEvents.h"
  2. #include "EPPC.h"
  3. #include "apple events.h"
  4. #include "generic open.h"
  5. #include "environment.h"
  6. #include "error.h"
  7. #include "kant.h"
  8.  
  9. static OSErr GotRequiredParameters(const AppleEvent *theAppleEvent);
  10. pascal OSErr HandleOAppEvent(const AppleEvent *theEvent, const AppleEvent *reply, long refCon);
  11. pascal OSErr HandleDocEvent(const AppleEvent *theEvent, const AppleEvent *reply, long refCon);
  12. pascal OSErr HandleQuitEvent(const AppleEvent *theEvent, const AppleEvent *reply, long refCon);
  13.  
  14. static    AEEventHandlerUPP gHandleOAppUPP;
  15. static    AEEventHandlerUPP gHandleDocUPP;
  16. static    AEEventHandlerUPP gHandleQuitUPP;
  17.  
  18. static OSErr GotRequiredParameters(const AppleEvent *theAppleEvent)
  19. {
  20.     OSErr myErr;
  21.     DescType returnedType;
  22.     Size actualSize;
  23.     
  24.     myErr = AEGetAttributePtr(theAppleEvent, keyMissedKeywordAttr, typeWildCard, &returnedType,
  25.                 nil, 0, &actualSize);
  26.     if (myErr == errAEDescNotFound)
  27.         return noErr;
  28.     else if (myErr == noErr)
  29.         return errAEParamMissed;
  30.     else
  31.         return myErr;
  32. }
  33.  
  34. pascal OSErr HandleOAppEvent(const AppleEvent *theEvent, const AppleEvent *reply, long refCon)
  35. {
  36.     #pragma unused(reply, refCon)
  37.     
  38.     OSErr theError;
  39.     
  40.     theError = GotRequiredParameters(theEvent);
  41.     return theError;
  42. }
  43.  
  44. pascal OSErr HandleDocEvent(const AppleEvent *theEvent, const AppleEvent *reply, long refCon)
  45. {
  46.     #pragma unused(theEvent, reply, refCon)
  47.     
  48.     OSErr theError;
  49.     AEDescList docList;
  50.     long itemsInList;
  51.     long index;
  52.     AEKeyword keyword;
  53.     DescType returnedType;
  54.     FSSpec theFileSpec;
  55.     Size actualSize;
  56.     
  57.     
  58.     theError = AEGetParamDesc(theEvent, keyDirectObject, typeAEList,  &docList);
  59.     if (theError == noErr)
  60.     {
  61.         theError = GotRequiredParameters(theEvent);
  62.         if (theError == noErr)
  63.         {
  64.             theError = AECountItems(&docList, &itemsInList);
  65.             if (theError == noErr)
  66.             {
  67.                 for (index = 1; index <= itemsInList; index++)
  68.                 {
  69.                     theError = AEGetNthPtr(&docList, index, typeFSS, &keyword, &returnedType,
  70.                                 (Ptr) &theFileSpec, sizeof(theFileSpec), &actualSize);
  71.                     if (theError == noErr)
  72.                     {
  73.                         if (refCon == kAEOpenDocuments)
  74.                             OpenTheFile(&theFileSpec);
  75.                         else
  76.                             PrintTheFile(&theFileSpec);
  77.                     };
  78.                 };
  79.             };
  80.         };
  81.         (void) AEDisposeDesc(&docList);
  82.     };
  83.     return theError;
  84. }
  85.  
  86. pascal OSErr HandleQuitEvent(const AppleEvent *theEvent, const AppleEvent *reply, long refCon)
  87. {
  88.     #pragma unused(reply, refCon)
  89.     
  90.     OSErr theError;
  91.     
  92.     theError = GotRequiredParameters(theEvent);
  93.     if (theError == noErr)
  94.     {
  95.         gDone=ShutDownTheProgram();
  96.         if (!gDone)
  97.             theError = userCanceledErr;
  98.     };
  99.     return theError;
  100. }
  101.  
  102. OSErr InstallRequiredAppleEvents(void)
  103. {
  104.     OSErr result;
  105.     
  106.     gHandleOAppUPP = NewAEEventHandlerProc(HandleOAppEvent);
  107.     FailNilUPP((UniversalProcPtr) gHandleOAppUPP);
  108.     gHandleDocUPP = NewAEEventHandlerProc(HandleDocEvent);
  109.     FailNilUPP((UniversalProcPtr) gHandleDocUPP);
  110.     gHandleQuitUPP = NewAEEventHandlerProc(HandleQuitEvent);
  111.     FailNilUPP((UniversalProcPtr) gHandleQuitUPP);
  112.  
  113.     result = AEInstallEventHandler(kCoreEventClass, kAEOpenApplication,
  114.                 gHandleOAppUPP, 0, false);
  115.     if (result == noErr)
  116.         result = AEInstallEventHandler(kCoreEventClass, kAEOpenDocuments,
  117.                     gHandleDocUPP, kAEOpenDocuments, false);
  118.     if (result == noErr)
  119.         result = AEInstallEventHandler(kCoreEventClass, kAEPrintDocuments,
  120.                     gHandleDocUPP, kAEPrintDocuments, false);
  121.     if (result == noErr)
  122.         result = AEInstallEventHandler(kCoreEventClass, kAEQuitApplication,
  123.                     gHandleQuitUPP, 0, false);
  124.     return result;
  125. }
  126.